﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using Telerik.Web.UI;
using SMA.GENTEv1;

public partial class Protegidos_Empresas_Cadastros__wf_wfBeneficiosCM : System.Web.UI.Page
{
    protected int idWfConfig = 0;
    protected int idTipoProcesso = 0;
    protected string nomeProcesso = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["IdWfConfig"] != null)
            idWfConfig = Int32.Parse(Request.QueryString["IdWfConfig"].ToString());

        if (Request.QueryString["IdTipoProcesso"] != null)
            idTipoProcesso = Int32.Parse(Request.QueryString["IdTipoProcesso"].ToString());

        if (Request.QueryString["NomeProcesso"] != null)
            nomeProcesso = Request.QueryString["NomeProcesso"].ToString();

        if (!IsPostBack)
        {
            ExibeParametros();
        }

        AtivaAjax();
    }

    #region Eventos

    protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
    {
        CarregarTudo();
    }

    #region Alerta
    protected void GridAlerta_DeleteCommand(object source, GridCommandEventArgs e)
    {
        int id = int.Parse((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["IdWfConfigParametro"].ToString());

        WorkflowConfigParametro wfParametro = new WorkflowConfigParametro(id);
        wfParametro.Excluir();

        CarregarTudo();
    }
    protected void GridAlerta_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridCommandItem)
        {
            int total = 0;

            if (e.Item.OwnerTableView.DataSourceCount != null)
                total = e.Item.OwnerTableView.DataSourceCount;

            if (total >= 4)
            {
                HtmlGenericControl btnInsert = e.Item.FindControl("dvGridInsert") as HtmlGenericControl;
                btnInsert.Visible = false;
            }
        }
        else if (e.Item is GridDataItem)
        {
            HyperLink editLink = (HyperLink)e.Item.FindControl("EditLink");
            editLink.Attributes["href"] = "#";
            editLink.Attributes["onclick"] = String.Format("return ShowUpdateAlerta('{0}','{1}');", "tpPagUnicoA", e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["IdWfConfigParametro"]);
        }
    }
    #endregion

    #region Pendencia
    protected void GridPendencia_DeleteCommand(object source, GridCommandEventArgs e)
    {
        int id = int.Parse((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["IdWfConfigParametro"].ToString());

        WorkflowConfigParametro wfParametro = new WorkflowConfigParametro(id);
        wfParametro.Excluir();

        CarregarTudo();
    }
    protected void GridPendencia_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridCommandItem)
        {
            int total = 0;

            if (e.Item.OwnerTableView.DataSourceCount != null)
                total = e.Item.OwnerTableView.DataSourceCount;

            if (total >= 4)
            {
                HtmlGenericControl btnInsert = e.Item.FindControl("dvGridInsert") as HtmlGenericControl;
                btnInsert.Visible = false;
            }
        }
        else if (e.Item is GridDataItem)
        {
            HyperLink editLink = (HyperLink)e.Item.FindControl("EditLink");
            editLink.Attributes["href"] = "#";
            editLink.Attributes["onclick"] = String.Format("return ShowUpdatePendencia('{0}','{1}');", "tpPagUnicoP", e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["IdWfConfigParametro"]);
        }
    }
    #endregion

    #endregion

    #region Metodos
    private void AtivaAjax()
    {
        WorkflowConfigParametro wfConfigParametro = new WorkflowConfigParametro();
        List<WorkflowConfigParametro> listWkConfigParametro = wfConfigParametro.SelectPor(idWfConfig, "Simples");

        foreach (WorkflowConfigParametro item in listWkConfigParametro)
        {
            RadAjaxManager1.AjaxSettings.AddAjaxSetting(RadAjaxManager1, GridAlerta, RadAjaxLoadingPanel1);
            RadAjaxManager1.AjaxSettings.AddAjaxSetting(RadAjaxManager1, GridPendencia, RadAjaxLoadingPanel1);
        }
    }
    private void ExibeParametros()
    {
        WorkflowConfigParametro wfConfigParametro = new WorkflowConfigParametro();
        List<WorkflowConfigParametro> listWkConfigParametro = wfConfigParametro.SelectPor(idWfConfig, "Simples");

        if (listWkConfigParametro.Count == 0)
        {
            dvAlertaPendencia.Visible = true;
            CarregarPagUnicoAlerta(0);
            CarregarPagUnicoAviso(0);
            hdIdWfConfig.Value = idWfConfig.ToString();
        }

        foreach (WorkflowConfigParametro item in listWkConfigParametro)
        {
            CarregarPagUnicoAlerta(idWfConfig);
            CarregarPagUnicoAviso(idWfConfig);
            hdIdWfConfig.Value = idWfConfig.ToString();
        }
    }
    private void CarregarTudo()
    {
        if (Convert.ToInt32(hdIdWfConfig.Value) != 0)
        {
            CarregarPagUnicoAlerta(Convert.ToInt32(hdIdWfConfig.Value));
            CarregarPagUnicoAviso(Convert.ToInt32(hdIdWfConfig.Value));
        }
    }
    private List<WorkflowConfigParametro> RetornaWfParametroAlerta(int id, string tipo)
    {
        WorkflowConfigParametro wfParametro = new WorkflowConfigParametro();
        List<WorkflowConfigParametro> listParametrosAlerta = wfParametro.SelectAlerta(id, tipo);

        return listParametrosAlerta;
    }
    private List<WorkflowConfigParametro> RetornaWfParametroAviso(int id, string tipo)
    {
        WorkflowConfigParametro wfParametro = new WorkflowConfigParametro();
        List<WorkflowConfigParametro> listParametrosAlerta = wfParametro.SelectAviso(id, tipo);

        return listParametrosAlerta;
    }

    #region Pagamento Unico
    private void CarregarPagUnicoAlerta(int _idWfConfig)
    {
        GridAlerta.DataSource = RetornaWfParametroAlerta(_idWfConfig, "Simples");
        GridAlerta.DataBind();
    }
    private void CarregarPagUnicoAviso(int _idWfConfig)
    {
        GridPendencia.DataSource = RetornaWfParametroAviso(_idWfConfig, "Simples");
        GridPendencia.DataBind();
    }
    #endregion

    #endregion
    
}
